package com.judian.smart.util;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;

import com.kaopiz.kprogresshud.KProgressHUD;

public class ProgressDialogUtils {


    private KProgressHUD ins;

    /*
     * 仿ios的圆形下载进度条
     *
     * yyx:该函数可重写
     *
     * @param context 显示的上下文
     * @param progress 进度条
     * @return 返回当前弹窗对象 以便有后续操作
     */
//	public static KProgressHUD getKProgressHUDDialog(Context context, int progress){
//
//		if (ins==null){
//			ins = KProgressHUD.create(context)
//					.setStyle(KProgressHUD.Style.ANNULAR_DETERMINATE)
//					.setLabel(context.getString(R.string.dateDown))
//					.setMaxProgress(100);
//		}
//		ins.show().setProgress(progress);
//		if (!ins.isShowing())
//			ins = null;//如果当前窗口消失，下次调用重新创建
//
//		return ins;
//	}

    /**
     * 加载等待窗口
     *
     * @param text 加载的文本提示
     * @return 当前弹窗对象 以便于后续操作
     */
    public KProgressHUD getLoadDialog(Context context, String text) {
        if (ins == null) {
            ins = KProgressHUD.create(context)
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setLabel(text)
                    .setCancellable(false)
                    .setAnimationSpeed(1)
                    .setDimAmount(0.5f);

        }
        ins.show();
        if (!ins.isShowing()) {
            ins = null;//如果当前窗口消失，下次调用重新创建
        }

        return ins;
    }


    /**
     * ProgressDialogInterface
     *
     * @param activity 显示弹窗所在的activity
     * @param message  显示的消息
     */
    public static void showProgressDialog(Activity activity, CharSequence message) {
        if (mProgressDialog == null) {
            mProgressDialog = ProgressDialog.show(activity, "", message);
            mProgressDialog.setOnKeyListener(keylistener);
        }
        mProgressDialog.show();
    }

    /**
     * 设置返回键监听
     */
    private static DialogInterface.OnKeyListener keylistener = new DialogInterface.OnKeyListener() {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                dismissProgressDialog();
                return true;
            } else {
                return false;
            }
        }
    };

    /**
     * 让dialog消失<br/>
     * 注意:请确定已经调用show
     */
    public static void dismissProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }


    private static ProgressDialog mProgressDialog;

}
